using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSRI.SmsService;
using MSRI.SmsSessions;
using MSRI.HelperClasses;
using Excel = Microsoft.Office.Interop.Excel;
namespace SMS_via_Excel_Sheet_Sample_2
{
    public partial class Form1 : Form
    {
        public SmsServiceClass SmsService = null;
        delegate void SMSService_OnDeliveryReportReceivedDelegate(DeliveryReportClass report);
        private Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = null;
        SmsSessions MySessionList;
        SmsMenu MyMenu = null;
        bool ServiceReady = false;
        public Form1()
        {
            InitializeComponent();
            try
            {
                SmsService = new SmsServiceClass("SMS_via_Excel_Sheet_Sample_2");
                SmsService.OnSmsReceived += new OnSmsReceivedDelegate(SMSService_OnSMSReceived);
                MySessionList = new SmsSessions();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.Exit();
            }
        }

        //
        // Your Code for Processing a received SMS goes here
        //
        SmsResponseClass SMSService_OnSMSReceived(SmsClass SMSMessage)
        {
            SmsResponseClass response = new SmsResponseClass();
            if (ServiceReady == true)
            {
                SmsSession MySession = MySessionList[SMSMessage.Number];
                if (MySession == null)
                {
                    MySession = new SmsSession();
                    MySession.TimeInterval = 30 * 60; // 30 min
                    MySession.SessionName = SMSMessage.Number;
                    MySession.SessionContacts.Add(new SmsSessionContact(SMSMessage.Number));
                    SmsMenu MySessionMenu = new SmsMenu(MyMenu);
                    MySession.SessionUserData = MySessionMenu;
                    MySession.AttachSessionDelegate(MySessionMenu.ProcessMessage);
                    MySessionList.Add(MySession);
                }
                response = MySession.CallSessionDelegate(SMSMessage);
                if (response.SessionState == ResponseSessionStateEnum.Delete)
                    MySessionList.Remove(MySession);
            }
            return response;
        }

        //
        // Your Code for Processing a deliveryreport goes here
        //
        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.FileName = "*.xls;*.xlsx"; 
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Application.DoEvents();
                // Here is the call to Open a Workbook in Excel 
                // It uses most of the default values (except for the read-only which we set to true)
                FileNameBox.Text = openFileDialog1.FileName;
                ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Excel.Workbook theWorkbook = ExcelApp.Workbooks.Open
                    (openFileDialog1.FileName, 0, true, 5,"","",
                            true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true,true,false);
                Excel.Worksheet workSheet = (Excel.Worksheet)theWorkbook.ActiveSheet;
                try
                {
                    int col = 1;
                    int keycol = -1;
                    int textcol = -1;
                    int responsecol = -1;
                    MyMenu = new SmsMenu();
                    MyMenu.MoreKey = "+";
                    while (((Excel.Range)workSheet.Cells[1, col]).Value2 != null)
                    {

                        string PropertyName = ((Excel.Range)workSheet.Cells[1, col]).Value2.ToString();
                        if (PropertyName.Trim().ToLower() == "header" && ((Excel.Range)workSheet.Cells[2, col]).Value2 != null)
                        {

                            MyMenu.Header = ((Excel.Range)workSheet.Cells[2, col]).Value2.ToString();
                        }
                        else if (PropertyName.Trim().ToLower() == "footer" && ((Excel.Range)workSheet.Cells[2, col]).Value2 != null)
                        {
                            MyMenu.Footer = ((Excel.Range)workSheet.Cells[2, col]).Value2.ToString();
                        }
                        else if (PropertyName.Trim().ToLower() == "key")
                        {
                            keycol = col;
                        }
                        else if (PropertyName.Trim().ToLower() == "text")
                        {
                            textcol = col;
                        }
                        else if (PropertyName.Trim().ToLower() == "responsestring")
                        {
                            responsecol = col;
                        }
                        col++;
                    }
                    if(keycol!=-1 && textcol!=-1 && responsecol!=-1)
                    {
                        int row = 2;
                        MenuListBox.Items.Clear();
                        MyMenu.ClearSubMenuItems();
                        while (((Excel.Range)workSheet.Cells[row,keycol]).Value2 != null &&
                            ((Excel.Range)workSheet.Cells[row, textcol]).Value2 != null &&
                            ((Excel.Range)workSheet.Cells[row, responsecol]).Value2 != null)
                        {
                            SmsMenuItem smsMenuItem = new SmsMenuItem();
                            smsMenuItem.Key = ((Excel.Range)workSheet.Cells[row, keycol]).Value2.ToString().ToUpper();
                            smsMenuItem.Text = ((Excel.Range)workSheet.Cells[row, textcol]).Value2.ToString();
                            smsMenuItem.ResponseString = ((Excel.Range)workSheet.Cells[row, responsecol]).Value2.ToString();
                            smsMenuItem.ResponseSessionState = ResponseSessionStateEnum.Reset;
                            MyMenu.AddSubMenuItem(smsMenuItem);
                            MenuListBox.Items.Add(smsMenuItem.Key);
                            MenuListBox.Items[row - 2].SubItems.Add(smsMenuItem.Text);
                            MenuListBox.Items[row - 2].SubItems.Add(smsMenuItem.ResponseString);
                            row++;
                        }
                        ServiceReady = true;
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(string.Format("Error reading the Excel sheet:{0}",ex.Message));
                }
                ExcelApp.Quit();
            }

        }
    }
}